package com.xm.zcf.action.action.product;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xm.zcf.domain.product.Product;
import com.xm.zcf.service.categorySecond.CategorySecondService;
import com.xm.zcf.service.product.ProductService;
import com.xm.zcf.util.PageBean;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */
@Controller
@Scope("prototype")
public class productAction extends ActionSupport implements ModelDriven<Product>{
    private Product model=new Product();
    private Integer page;
    private Integer limit=10;
    private Integer pid;
    private String imageFileName;
    private File image;
    public String getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String imagefileName) {
        this.imageFileName = imagefileName;
    }


    @Resource
    private CategorySecondService categorySecondService;
    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    @Resource
    private ProductService productService;
    public String addUI(){
        return "addUI";
    }
    public String editUI(){
        Product product=productService.findById(model.getPid());
        ActionContext.getContext().getValueStack().push(product);
        List cslist=categorySecondService.findSecondAll();
        ActionContext.getContext().put("cslist",cslist);
        return "editUI";
    }
    public String findAll(){
        List<Product> categorylist=productService.findAll();
        ActionContext.getContext().put("pList",categorylist);
        return "list";
    }
    public String findPage(){
        PageBean<Product> productPageBean=productService.findPage(page,limit);
        ActionContext.getContext().getValueStack().push(productPageBean);
        return "list";
    }
    public String add(){
        String realPath= ServletActionContext.getServletContext().getRealPath("products/5");
        if(image!=null){
            File file=new File(new File(realPath),imageFileName);
            try{
                FileUtils.copyFile(image,file);
                model.setImage("products/5/"+imageFileName);
                productService.save(model);
            }catch (IOException e){
                e.printStackTrace();
                return "tolist";
            }
        }
        return "tolist";
    }
    public String edit(){
        return"list";
    }
    @Override
    public Product getModel() {
        return model;
    }
}
